﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Toolkit.Web.Infastructure;
using Toolkit.Web.ExtensionMethods;

namespace Toolkit.Web.ViewComponents.Panel
{
    public sealed class PanelBuilder : ViewComponentBuilderBase<Panel>
    {
        public PanelBuilder(Panel component)
            : base(component)
        {
            if (component == null)
            {
                throw new ArgumentNullException("component");
            }
        }

        public PanelBuilder PrimaryHeader(Action<object, IViewComponentWriter> template)
        {
            Component.PrimaryHeader = new HtmlTemplate()
            {
                ActionTemplate = template
            };

            return this;
        }

        public PanelBuilder PrimaryHeader(Func<object, object> template)
        {
            Component.PrimaryHeader = new HtmlTemplate()
            {
                InlineTemplate = template
            };

            return this;
        }

        public PanelBuilder SecondaryHeader(Action<object, IViewComponentWriter> template)
        {
            Component.SecondaryHeader = new HtmlTemplate()
            {
                ActionTemplate = template
            };

            return this;
        }

        public PanelBuilder SecondaryHeader(Func<object, object> template)
        {
            Component.SecondaryHeader = new HtmlTemplate()
            {
                InlineTemplate = template
            };

            return this;
        }

        public PanelBuilder Content(Action<object, IViewComponentWriter> template)
        {
            return Content(template, null);
        }

        public PanelBuilder Content(Action<object, IViewComponentWriter> template, object attributes)
        {
            Component.Body = new HtmlTemplate()
            {
                ActionTemplate = template
            };

            Component.BodyHtmlAttributes.Clear();

            if (attributes != null)
            {
                Component.BodyHtmlAttributes.Merge(attributes);
            }

            return this;
        }

        public PanelBuilder Content(Func<object, object> template)
        {
            return Content(template, null);
        }

        public PanelBuilder Content(Func<object, object> template, object attributes)
        {
            Component.Body = new HtmlTemplate()
            {
                InlineTemplate = template
            };

            Component.BodyHtmlAttributes.Clear();

            if (attributes != null)
            {
                Component.BodyHtmlAttributes.Merge(attributes);
            }

            return this;
        }

        public PanelBuilder Collapsible(bool isCollapsible)
        {
            return Collapsible(isCollapsible, false);
        }

        public PanelBuilder Collapsible(bool isCollapsible, bool collapseOnRender)
        {
            Component.Collapsible = isCollapsible;
            Component.CollapseOnRender = collapseOnRender;

            return this;
        }

        public PanelBuilder ClientEventHandlers(Action<ClientEventHandlers> handlers)
        {
            if (handlers != null)
            {
                handlers(Component.ClientEventHandlers);
            }
            return this;
        }
    }
}
